/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.POInternalDAOInterface;
import com.wii.elpro.dao.hb.POInternalDAOImpl;
import com.wii.elpro.entity.POInternal;
import com.wii.elpro.entity.POInternalItem;
import com.wii.elpro.entity.Quotation;
import com.wii.elpro.entity.QuotationItem;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.POInternalForm;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author City_Z
 */
public class POInternalMarketingSaveAllAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        POInternalForm f = (POInternalForm)form;
        POInternalDAOInterface dao = new POInternalDAOImpl();
        Quotation q = (Quotation)sess.getAttribute("q");
        List list = new ArrayList();
        try{
            POInternal p = new POInternal();
            p.setTglPo(new Date());
            p.setPoCustomer(request.getParameter("poCustomer"));
            p.setQuotation(q);
            p.setApproveByMarketing(Byte.parseByte("1"));
            p.setApproveByFinance(Byte.parseByte("0"));
            p.setApproveByFactory(Byte.parseByte("0"));
            for(int x=0;x<q.getItems().size();x++){
                QuotationItem qi = (QuotationItem)q.getItems().get(x);
                POInternalItem pi = new POInternalItem();
                pi.setProduk(qi.getProduk());
                pi.setJumlah(qi.getJumlah());
                list.add(pi);
            }
            p.setItems(list);
            dao.save(p);
            sess.removeAttribute("q");
        }catch(Exception e){
            e.printStackTrace();
        }
        return mapping.findForward(SUCCESS);
        
    }
}